package com.example.demo.zookeeper.mytest.config;

import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * @author liweizhi
 * @date 2020/6/23
 */
public class ZkUtil {
    public static ZooKeeper zk;

    public static final String configPath = "/appConfig";

    private static final String myRoot = "/mytest";
    private static final String connectString = "192.168.200.11:2181,192.168.200.12:2181,192.168.200.13:2181,192.168.200.14:2181";
    private static final int sessionTimeout = 30000;
    private static final CountDownLatch cdl = new CountDownLatch(1);

    public static ZooKeeper initZookeeper() {
        try {
            DefaultWatcher watcher = new DefaultWatcher();
            watcher.setCdl(cdl);
            zk = new ZooKeeper(connectString + myRoot, sessionTimeout, watcher);
            cdl.await();
            return zk;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }
}
